using CfgTable;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using System;

namespace IQIGame.Onigao.GamePlay
{
    public class UIFunctionOn : UIBaseWindow
    {
        #region Template Generate,don't modify
        protected class UIB_UIFunctionOn
        {
            #region ObjectBinding Generate
            public IQIGame.Onigao.Framework.ExText textUnlock { protected set; get; }
            public IQIGame.Onigao.Framework.ExText textAutoClose { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("textUnlock", out var __tbv0);
                this.textUnlock = __tbv0;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("textAutoClose", out var __tbv1);
                this.textAutoClose = __tbv1;
            }
            #endregion ObjectBinding Generate 
        }
        #endregion Template Generate,don't modify

        #region Window Config
        /// <summary>
        /// 静态配置
        /// </summary>
        private static UICreateConfig _createConfig = new()
        {
            prefabName = nameof(UIFunctionOn),
            layer = EUILayer.Notify,
        };

        /// <summary>
        /// 创建UI的配置
        /// </summary>
        public override IUICreateConfig createConfig => _createConfig;
        #endregion Window Config

        #region fields

        private CfgFunctionType cfgFunction;
        private Action actionOnClose;

        private float cooldown;
        #endregion fields

        #region properties

        protected UIB_UIFunctionOn ui { get; set; }
        protected UIMsgDispatcher uiMsgDispatcher => this._msgDispatcher as UIMsgDispatcher;

        #endregion properties

        protected override void BeforeInit()
        {
            ui = new UIB_UIFunctionOn();
            ui.InitBinding(this.csObjBind);
        }

        protected override void OnInit()
        {

        }

        protected override void OnShow(UIBaseData data)
        {
            SetData(data as UD_FunctionOn);
            ShowView();
        }

        protected override void OnHide()
        {

        }

        /// <summary>
        /// 添加UGUI侦听
        /// </summary>
        protected override void AddUGUIListener()
        {

        }

        /// <summary>
        /// 添加全局侦听（MsgDispatcher）
        /// </summary>
        protected override void AddMsgListeners()
        {

        }

        protected override void OnDispose()
        {
            actionOnClose?.Invoke();
            actionOnClose = null;
        }

        #region methonds

        private void SetData(UD_FunctionOn data)
        {
            cfgFunction = TableCenter.functionType.Get(data.functionTypeId);
            actionOnClose = data.onFormUIClose;

            cooldown = APIUIFunctionOn.CD_AutoCloseUISec;
            isUpdate = true;
        }

        private void ShowView()
        {
            ui.textUnlock.text = cfgFunction.Name;

            ShowAutoClose();
        }

        private void ShowAutoClose()
        {
            ui.textAutoClose.text = APIUIFunctionOn.GetAutoCloseCDStr(cooldown);
        }
        #endregion methonds

        protected override void OnUpdate(float dt, float unscaledDt)
        {
            ShowAutoClose();

            cooldown -= dt;
            if (cooldown <= 0)
            {
                isUpdate = false;
                Close();
            }
        }
    }
}
